import permissionStore from '@/store/permission';
/**
 * 界面按钮权限控制
 */
export const permissions = {
    mounted(el: HTMLElement, binding: any) {
        let permissionBox = permissionStore();
        const permissions = permissionBox.permissions;
        let { value } = binding;
        if (value && value instanceof Array && value.length > 0) {
            const permissionRules = value
            const isPermission = permissions.some((permision:string) => {
                return permissionRules.includes(permision)
            })
            if (!isPermission) {
                // el.parentNode && el.parentNode.removeChild(el)
                el.setAttribute('disabled','true')
                // return false;
            }
        } else {
            return false;
            throw new Error(`need permissions! Like v-permission="['edit','search']"`)
        }
    }
}

//权限 disabled按钮
export const hasPermission = (permissionRules: Array<string>) => {
    let permissionBox = permissionStore();
    const permissions = permissionBox.permissions;
    if (permissionRules && permissionRules instanceof Array && permissionRules.length > 0) {
        const isPermission = permissions.some((permision:string) => {
            return permissionRules.includes(permision)
        })
        return !isPermission;
    } else {
        throw new Error(`:disabled="hasPerm['edit','search']"`)
    }
}
